#ifndef _ICAR_MSG_H
#define _ICAR_MSG_H

#if defined(__cplusplus)
extern "C" {
#endif

#include "icar.h"
#include "icar_end_msg.h"
#include <stdint.h>


#define SUBPACKAGE	(1 << 13)

#define ENCRYPT_RSA		1
#define ENCRYPT_NONE	0

#define ENCRYPT(p)	((p & 0x7) << 10)
#define ENCRYPT_WAY(p)	((p >> 10) & 0x7)

#define MSG_LENGTH(p)	(p & 0x03ff)

#define MAX_PKG_CNT		0xffff

#define READ_BUF_LEN		4096
#define READ_MAX_LEN		8192
#define	DATA_MAX_LEN		1024
#define IP_ADDR_LEN			17


#define ICAR_MSG(p, q, i, s)	do { \
						q++, i++; \
						p = q; \
						while (*q != ',' && *q != '#' && i < s) \
							q++, i++; \
						} while (0);

struct univer_res {
	uint16_t sequence;
	uint16_t id;
	uint8_t result;
};

struct supplement_req {
	uint16_t sequence;
	uint8_t count;
	uint8_t *data;
};

struct end_register {
	uint16_t province;
	uint16_t city;
	uint8_t product[5];
	uint16_t end_type[20];
	uint8_t end_id[7];
	uint8_t plate_color;
	char *plate_mark;
};

struct regiser_res {
	uint16_t sequence;
	uint8_t result;
	char *authcode;
	int len;
};

typedef struct {
	int clientSock;
	char clientAddr[IP_ADDR_LEN];
	char  number[13];
} request;

#define MSG_FLAG_V1         0x5601
#define MSG_FLAG_V4         0x5604
#define MSG_FLAG_V8         0x5608
#define MSG_FLAG_S6         0x5306
#define MSG_FLAG_S17        0x5317
#define MSG_FLAG_S20        0x5320
#define MSG_FLAG_S32        0x5332
#define MSG_FLAG_S81        0x5381
#define MSG_FLAG_R7         0x5207
#define MSG_FLAG_R8         0x5208
#define MSG_FLAG_I3         0x4903
#define MSG_FLAG_I7         0x4907
#define MSG_FLAG_I10        0x4910
#define MSG_FLAG_I11        0x4911
#define MSG_FLAG_I12        0x4912
#define MSG_FLAG_I13        0x4913
#define MSG_FLAG_I14        0x4914


int icar_create_server(int type, int port, const char *address);

int icar_get_connection(int fd, struct timeval *);

int _icar_net_read(int sock, uint8_t *buf, int len);

int icar_readPkg(uint8_t *,int , uint8_t *, size_t );

int icar_process_request(int, uint8_t *src, size_t len);

int icar_parse_recv(int, uint8_t *msg, size_t size);



#if defined(__cplusplus)
}
#endif

#endif
